package ee.az.test.lighting;

/**
 * @author Artur Zinatullin (artur.zinatullin@gmail.com) 2010
 */
public class Bordificator {
	
	private static final char[] BORDER_CHARS =
		("#*************************************************************──" +
		"│**********************************************│***************└" +
		"****************************************************************" +
		"******************************┤│***********│******************┼└" +
		"**************************************************************┼─" +
		"****************************************************************" +
		"***********************************************│****************" +
		"**********************┤│*********************│*****─*****─***┐**").toCharArray();

	public static void bordify(IconMap<IconSpot> map) {
		for (int y=0; y<map.getHeight(); y++) {
			for (int x=0; x<map.getWidth(); x++) {
				IconSpot value = map.getValue(x, y);
				if (isWall(value)) {
					value.setIcon(getBorderIcon(map, x, y));
				}
			}
		}
	}
	
	private static char getBorderIcon(IconMap<IconSpot> map, int x, int y) {
		int index = 0;
		for (int q=1; q<10; q++) {
			IconSpot neighbour = map.getNeighbour(x, y, q);
			if (isWall(neighbour) && q != 5) {
				index |= 1 << (q < 5 ? q-1 : q-2);
			}
		}
		System.out.println("[" + x + "," + y + "] = " + index + " " + BORDER_CHARS[index]);
		return BORDER_CHARS[index];
	}

	private static boolean isWall(IconSpot spot) {
		return spot == null ? true : spot.getIcon() != '.';
	}
}
